package com.macasa.panel.entity.impl;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.ForeignKey;

import com.google.common.base.Objects;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.ISGBDService;
import com.macasa.panel.entity.api.ISGBDServiceDatabase;
import com.macasa.panel.entity.api.ISGBDServiceUser;
import com.macasa.panel.entity.api.IStunnelService;

@Entity
@Table(name = "sgbd_service")
@Getter
@Setter
@ToString(callSuper = true)
public class SGBDService extends Service implements ISGBDService {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long			serialVersionUID	= -8884734865203179271L;

	@Column(name = "master_user", nullable = false)
	private String						masterUser;

	@Column(name = "master_password", nullable = false)
	private String						masterPassword;

	@Column(name = "key", nullable = true)
	private Integer						key;

	@OneToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, targetEntity = com.macasa.panel.entity.impl.StunnelService.class)
	@JoinColumn(name = "fk_stunnel_service", nullable = true, unique = false)
	@ForeignKey(name = "fk_sgbd_service_01")
	private IStunnelService				stunnelService;

	@OneToMany(mappedBy = "sgbdService", cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.SGBDServiceUser.class)
	@ForeignKey(name = "fk_sgbd_service_user_01")
	private List<ISGBDServiceUser>		users;

	@OneToMany(mappedBy = "sgbdService", cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.SGBDServiceDatabase.class)
	@ForeignKey(name = "fk_sgbd_service_database_02")
	private List<ISGBDServiceDatabase>	databases;

	@Column(name = "port", nullable = false)
	private Integer						port;

	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof SGBDService)) {
			return false;
		}

		final SGBDService other = CastHelpers.cast(obj, SGBDService.class);

		return super.equals(obj) && Objects.equal(this.masterUser, other.masterUser) && Objects.equal(this.masterPassword, other.masterPassword)
				&& Objects.equal(this.key, other.key) && Objects.equal(this.stunnelService, other.stunnelService) && Objects.equal(this.users, other.users)
				&& Objects.equal(this.databases, other.databases) && Objects.equal(this.port, other.port);
	}

	@Override
	public int hashCode() {
		return super.hashCode() + Objects.hashCode(this.masterUser, this.masterPassword, this.key, this.stunnelService, this.users, this.databases, this.port);
	}
}
